package com.app.controller;

import com.app.model.AdminUserModel;
import com.app.model.NoticeModel;
import com.app.service.AdminUserService;
import com.app.service.FormService;
import com.app.service.NoticeService;
import com.app.service.UserService;
import com.app.util.CommonHelp;
import com.app.util.DateUtil;
import com.app.util.Result;
import com.app.util.TokenUtils;
import com.app.vo.CensusVo;
import com.app.vo.MsgVo;
import com.app.vo.NoticeVo;
import com.app.vo.XyVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

@RestController
@Controller
@RequestMapping("/api/home")
public class CensusBehindController {
    @Autowired
    private FormService formService;
    @Autowired
    private UserService userService;


    @Autowired
    private AdminUserService adminUserService;

    /**
     * 统计数量
     *
     * @return
     * @throws Exception
     */
    @GetMapping("/getBehindCensus")
    public Result<Object> getBehindCensus(HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        if (userId.equals("false")){
            return Result.error(405,"token已过期");
        }
        AdminUserModel user = adminUserService.getUserByUserId(Integer.valueOf(userId));
        if (user == null) {
            return Result.error(406, "用户不存在");
        }
        CensusVo vo = new CensusVo();
        Integer todayFormCount = formService.getModelTodayCount(false);//今日随手拍数量
        Integer todayRegisterCount = formService.getModelTodayCount(true);
        ;//今日立案受理量
        Integer todayUserVisitCount = 0;//今日用户访问量
        Integer todayAddUserCount = userService.getModelTodayCount();//今日新增用户
        vo.setTodayFormCount(todayFormCount);
        vo.setTodayAddUserCount(todayAddUserCount);
        vo.setTodayRegisterCount(todayRegisterCount);
        vo.setTodayUserVisitCount(todayUserVisitCount);

        Integer yesterdayFormCount = formService.getModelYesterdayCount(false);//昨日随手拍数量
        Integer yesterdayRegisterCount = formService.getModelYesterdayCount(true);//昨日立案受理量
        Integer yesterdayUserVisitCount = 0;//昨日用户访问量
        Integer yesterdayAddUserCount = userService.getModelYesterdayCount();//昨日新增用户
        vo.setYesterdayFormCount(yesterdayFormCount);
        vo.setYesterdayRegisterCount(yesterdayRegisterCount);
        vo.setYesterdayUserVisitCount(yesterdayUserVisitCount);
        vo.setYesterdayAddUserCount(yesterdayAddUserCount);


        //目前不明确状态代码，明确后修改参数
        Integer foundationNoCount = formService.getModelByQuestionTypeCount(2, null);//基础设施不完善
        Integer orderNoCount = formService.getModelByQuestionTypeCount(3, null);//公共秩序不文明
        Integer hygieneNoCount = formService.getModelByQuestionTypeCount(4, null);//公共环境卫生不文明
        Integer unobstructedNoCount = formService.getModelByQuestionTypeCount(5, null);//交通不文明
        Integer civilizationNoCount = formService.getModelByQuestionTypeCount(6, null);//居民小区不文明
        List<MsgVo> msgVoList = new ArrayList<>();
        MsgVo msgVo = new MsgVo();
        msgVo.setName("基础设施不完善");
        msgVo.setValue(foundationNoCount);
        msgVoList.add(msgVo);
        MsgVo msgVo2 = new MsgVo();
        msgVo2.setName("公共秩序不文明");
        msgVo2.setValue(orderNoCount);
        msgVoList.add(msgVo2);
        MsgVo msgVo3 = new MsgVo();
        msgVo3.setName("公共环境卫生不文明");
        msgVo3.setValue(hygieneNoCount);
        msgVoList.add(msgVo3);
        MsgVo msgVo4 = new MsgVo();
        msgVo4.setName("交通不文明");
        msgVo4.setValue(unobstructedNoCount);
        msgVoList.add(msgVo4);
        MsgVo msgVo5 = new MsgVo();
        msgVo5.setName("居民小区不文明");
        msgVo5.setValue(civilizationNoCount);
        msgVoList.add(msgVo5);
        vo.setCategoryCensusList(msgVoList);

        //目前不明确状态代码，明确后修改参数
        Integer waitCount = formService.getModelByQuestionTypeCount(null, 0);//待处理
        Integer registerCount = formService.getModelByQuestionTypeCount(null, 1);//立案受理
        Integer rejectCount = formService.getModelByQuestionTypeCount(null, 2);//驳回
        Integer closeCount = formService.getModelByQuestionTypeCount(null, 3);//结案

        List<MsgVo> handleList = new ArrayList<>();
        MsgVo msgVo6 = new MsgVo();
        msgVo6.setName("待处理");
        msgVo6.setValue(waitCount);
        handleList.add(msgVo6);
        MsgVo msgVo7 = new MsgVo();
        msgVo7.setName("立案受理");
        msgVo7.setValue(registerCount);
        handleList.add(msgVo7);
        MsgVo msgVo8 = new MsgVo();
        msgVo8.setName("驳回");
        msgVo8.setValue(rejectCount);
        handleList.add(msgVo8);
        MsgVo msgVo9 = new MsgVo();
        msgVo9.setName("结案");
        msgVo9.setValue(closeCount);
        handleList.add(msgVo9);
        vo.setHandleCensusList(handleList);
        return Result.success(vo);
    }


    /**
     * 统计数量(柱状图)
     *
     * @return
     * @throws Exception
     */
    @GetMapping("/getBehindFormCensus")
    public Result<Object> getBehindFormCensus(HttpServletRequest request, Integer type) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        if (userId.equals("false")){
            return Result.error(405,"token已过期");
        }
        AdminUserModel user = adminUserService.getUserByUserId(Integer.valueOf(userId));
        if (user == null) {
            return Result.error(406, "用户不存在");
        }

        Map<String, int[]> countMap = new HashMap<>();
        Map<String, Integer> countData = new HashMap<>();
        for (int i = 0; i < type; i++) {
            int allCount = 0;
            String time = DateUtil.formatDate(DateUtil.addDays(new Date(), -i));
            int count2 = formService.getModelCountByTime(time, 2);
            int count3 = formService.getModelCountByTime(time, 3);
            int count4 = formService.getModelCountByTime(time, 4);
            int count5 = formService.getModelCountByTime(time, 5);
            int count6 = formService.getModelCountByTime(time, 6);
            int value[] = new int[5];
            value[0] = count2;
            value[1] = count3;
            value[2] = count4;
            value[3] = count5;
            value[4] = count6;
            countMap.put(time, value);
            allCount += count2 + count3 + count4 + count5 + count6;
            countData.put(time, allCount);

        }
        CensusVo vo = new CensusVo();
        vo.setCountsData(countMap);
        vo.setCountData(countData);
        return Result.success(vo);
    }

    /**
     * 统计数量(柱状图)
     *
     * @return
     * @throws Exception
     */
    @GetMapping("/getBehindFormStatusCensus")
    public Result<Object> getBehindFormStatusCensus(HttpServletRequest request, Integer type) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        if (userId.equals("false")){
            return Result.error(405,"token已过期");
        }
        AdminUserModel user = adminUserService.getUserByUserId(Integer.valueOf(userId));
        if (user == null) {
            return Result.error(406, "用户不存在");
        }

        Map<String, int[]> countMap = new HashMap<>();
        Map<String, Integer> countData = new HashMap<>();
        for (int i = 0; i < type; i++) {
            int allCount = 0;
            String time = DateUtil.formatDate(DateUtil.addDays(new Date(), -i));
            int count2 = formService.getModelCountByTimeAndStatus(time, 2);
            int count3 = formService.getModelCountByTimeAndStatus(time, 3);
            int count4 = formService.getModelCountByTimeAndStatus(time, 4);
            int value[] = new int[3];
            value[0] = count2;
            value[1] = count3;
            value[2] = count4;
            countMap.put(time, value);
            allCount += count2 + count3 + count4;
            countData.put(time, allCount);


        }
        CensusVo vo = new CensusVo();
        vo.setCountsData(countMap);
        vo.setCountData(countData);
        return Result.success(vo);
    }

    /**
     * 统计数量(三角)
     *
     * @return
     * @throws Exception
     */
    @GetMapping("/getBehindTriangleCensus")
    public Result<Object> getBehindTriangleCensus(HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        if (userId.equals("false")){
            return Result.error(405,"token已过期");
        }
        AdminUserModel user = adminUserService.getUserByUserId(Integer.valueOf(userId));
        if (user == null) {
            return Result.error(406, "用户不存在");
        }
        CensusVo vo = new CensusVo();
        Integer regoCount = userService.getModelCount();//注册用户数
        Integer formCount = formService.getModelCount(null);//随手拍数量
        Integer registerCount = formService.getModelCount(2);//立案受理
        vo.setRegoCount(regoCount);
        vo.setFormCount(formCount);
        vo.setRegisterCount(registerCount);
        vo.setBriskCount(0);
        vo.setAcceptCount(0);
        vo.setVisitCount(0);
        return Result.success(vo);
    }

    /**
     * 统计数量(折线)
     *
     * @return
     * @throws Exception
     */
    @GetMapping("/getBehindUserCensus")
    public Result<Object> getBehindUserCensus(HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        if (userId.equals("false")){
            return Result.error(405,"token已过期");
        }
        AdminUserModel user = adminUserService.getUserByUserId(Integer.valueOf(userId));
        if (user == null) {
            return Result.error(406, "用户不存在");
        }
        Map<String, Integer> countData = new HashMap<>();
        for (int i = 0; i < 30; i++) {
            String time = DateUtil.formatDate(DateUtil.addDays(new Date(), -i));
            int count = userService.getModelCountByTime(time);
            countData.put(time, count);

        }
        CensusVo vo = new CensusVo();
        vo.setCountData(countData);
        return Result.success(vo);
    }



}
